var demo;
$(function() {
	//开启验证
	demo = $("#editForm").Validform({
		tiptype: 3
	});
	addDialogButton("保存",save);
	addDialogButton("打印",printPage);

	laydate.render({
	    trigger:'click',
	    elem: '#sub_time' //指定元素
	});
	
});

var save = function() {
	if(!demo.check()) {
		$('.Validform_error:first').focus();
		return;
	}
	var datas = getFormJson();
	if($('#id').val()==""){
		showDialogModalConfirm('confirm','请输入文件名称','<input type="text" class="new_name" style="background-color:#eee; text-indent:5px;"/><script>$(".new_name").focus()</script>' ,'确认','取消',function(){ 
			var load_alert=loadAlert('保存中...');
			datas.name=$('.new_name').val();
			if(datas.name==''){
				window.parent.topMessagePrompt('error','请填写文件名');
				return;
			}
			$.ajax({
				url: "Admin/SupervisionOther/tableSave1",
				type: "post",
				data: datas,
				dataType:'json',
				success: function(data) {
					closeAlert(load_alert);
					if(data.status) {
						window.parent.topMessagePrompt('success',data.msg);
						hideDialogModal();
						var t=JSON.parse(data.v);
						//改变文件夹is_have
						var r=JSON.parse(data.remark);
						if(r.is_add){
							$('.document-demo .img', parent.document).attr('data-id',t.id);
							$('.document-demo .delete', parent.document).attr('data-id',t.id);
							$('.document-demo .reset-name', parent.document).attr('data-id',t.id);
							$('.document-demo .title', parent.document).html(sub_str(t.name,6)).attr('title',t.name);
							$('.document-box', parent.document).append($('.document-demo', parent.document).html());
						}
						else{
							$('.document-box .reset-name', parent.document).each(function(){
								if($(this).data('id')==t.id){
									$(this).parents('.document-content').find('.title').html(sub_str(t.name,6)).attr('title',t.name);
								}
							});
						}
						$('.folder-content .img', parent.document).each(function(){
							if($(this).data('id')==$('#now-folder-id', parent.document).val()){
								$(this).attr('src',r.folder_img);
							}
						});
					} else {
						window.parent.topMessagePrompt('error',data.msg);
					}
				}
			});
		});
	}
	else{
		var load_alert=loadAlert('保存中...');
		datas.name='';
		$.ajax({
			url: "Admin/SupervisionOther/tableSave1",
			type: "post",
			data: datas,
			dataType:'json',
			success: function(data) {
				closeAlert(load_alert);
				if(data.status) {
					window.parent.topMessagePrompt('success',data.msg);
					hideDialogModal();
					var t=JSON.parse(data.v);
					//改变文件夹is_have
					var r=JSON.parse(data.remark);
					if(r.is_add){
						$('.document-demo .img', parent.document).attr('data-id',t.id);
						$('.document-demo .delete', parent.document).attr('data-id',t.id);
						$('.document-demo .reset-name', parent.document).attr('data-id',t.id);
						$('.document-demo .title', parent.document).html(sub_str(t.name,6)).attr('title',t.name);
						$('.document-box', parent.document).append($('.document-demo', parent.document).html());
					}
					else{
						$('.document-box .reset-name', parent.document).each(function(){
							if($(this).data('id')==t.id){
								$(this).parents('.document-content').find('.title').html(sub_str(t.name,6)).attr('title',t.name);
							}
						});
					}
					$('.folder-content .img', parent.document).each(function(){
						if($(this).data('id')==$('#now-folder-id', parent.document).val()){
							$(this).attr('src',r.folder_img);
						}
					});
				} else {
					window.parent.topMessagePrompt('error',data.msg);
				}
			}
		});
	}

	
	
}

var printPage = function () { 
	window.print()
}

function exportTable(){
	swal({
		title: "请确认",
		text: '导出可能需要一定的时间，确认导出？',
		type: "warning",
		showCancelButton: true,
		closeOnConfirm: true,
		confirmButtonText: "确定",
		cancelButtonText: '取消',
		confirmButtonColor: "#ec6c62"
	}, function(isConfirm) {
		if(isConfirm) {
			$.ajax({
				type: "post",
				url: "Admin/SupervisionOther/tableExport1",
				data:{id:$('#id').val()},
				dataType:'json',
				async: true,
				success: function(data) {
					if(data.status) {
	                    window.parent.topMessagePrompt('success',data.msg);
	                    window.parent.refreshTable();
	                    location.href=data.v;
					} else {
						window.parent.topMessagePrompt('error',data.msg);
					}
				}
			});
		}
	});
}
